<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;
use GuzzleHttp\Client as GuzzleClient;
use Encore\Admin\Facades\Admin;
use App\Models\Order;
use App\Models\Client;
use Illuminate\Support\Facades\Request;

class SupplierController extends Controller
{
    public function index()
    {
        return Admin::content(function (Content $content){
            $content->header('供应商管理');
            $content->description('供应商设置');
            $articleView = view('admin.supplier')->render();
            $content->row($articleView);
            admin_toastr('提示', 'success');
        });
    }
    //发送请求
    public function guzzle($url, $params)
    {
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,

        ]);
        return $client->request('GET', $url, $params);
    }

    public function guzzle_post($url, $params){
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,
        ]);
        return $client->request('POST', $url, $params);
    }
    //获取供应商的列表数据
    public function getSuppliers()
    {
        $get_url = env('RISKDECISION') . '/risk/Config/GetConfigSupplier';

        $params = [
            'headers' => [
                'ident' => 'production'
            ],
        ];

        $config_data = $this->guzzle($get_url, $params);
        $res = json_decode($config_data->getBody()->getContents(), true);
        $response = [
            "code" => 0,
            "msg" => "",
            "count" => count($res['data']),
            "data" => $res['data']
        ];
        return $response;
    }
    //添加供应商
    public function supplieradd(){
        $method = Request::Method();
        if($method == 'GET'){
            $get_url = env('RISKDECISION') . '/risk/Config/GetSupplierList';

            $params = [
                'headers' => [
                    'ident' => 'production'
                ],
            ];

            $config_data = $this->guzzle($get_url, $params);
            $res = json_decode($config_data->getBody()->getContents(), true);

            return view('admin.supplieradd', compact('res'));
        }elseif($method == 'POST'){
            $rec = Request::all();
            unset($rec['_url']);

            $get_url = env('RISKDECISION') . '/risk/Config/SaveSupplier';

            $params = [
                'json' => $rec,
                'headers' => [
                    'ident' => 'production'
                ],
            ];

            $config_data = $this->guzzle_post($get_url, $params);
            $res = json_decode($config_data->getBody()->getContents(), true);
            if($res['rspCode'] == 0){
                //向业务系统通知通道的改变
                $client = Client::where('sign', $rec['user_id'])->first();
                $protocol = $client->protocol;
                $client_url = $client->client_url;
                $send_url = $protocol.'://'.$client_url.'/api/common/edit/supplier';
                $params = [
                    'form_params' => $rec,
                    'headers' => [
                        'ident' => 'production'
                    ]
                ];
                $yewures = $this->guzzle_post($send_url, $params);
                $yewuresponse = json_decode($yewures->getBody()->getContents(), true);

                if($yewuresponse['code'] == 0){
                    return ['code' => 0, 'text' => '保存成功'];
                }else{
                    return ['code' => -1, 'text' => '保存失败'];
                }
            }
        }
    }
    //编辑应商
    public function supplieredit(){
        $method = Request::Method();
        if($method == 'GET'){
            $user_id = Request::input('user_id');

            $get_url = env('RISKDECISION') . '/risk/Config/GetSupplierList';

            $params = [
                'headers' => [
                    'ident' => 'production'
                ],
            ];

            $config_data = $this->guzzle($get_url, $params);
            $res = json_decode($config_data->getBody()->getContents(), true);

            $get_url = env('RISKDECISION') . '/risk/Config/GetConfigSupplierDetail';

            $params = [
                'query' => [
                  'user_id' => $user_id,
                ],
                'headers' => [
                    'ident' => 'production'
                ],
            ];

            $config_data = $this->guzzle($get_url, $params);
            $response = json_decode($config_data->getBody()->getContents(), true);
//
            return view('admin.supplieredit', compact('res', 'response'));
        }elseif($method == 'POST'){
            $rec = Request::all();
            unset($rec['_url']);

            $get_url = env('RISKDECISION') . '/risk/Config/SaveSupplier';

            $params = [
                'json' => $rec,
                'headers' => [
                    'ident' => 'production'
                ],
            ];

            $config_data = $this->guzzle_post($get_url, $params);
            $res = json_decode($config_data->getBody()->getContents(), true);
            if($res['rspCode'] == 0){
                //向业务系统通知通道的改变
                $client = Client::where('sign', $rec['user_id'])->first();
                $protocol = $client->protocol;
                $client_url = $client->client_url;
                $send_url = $protocol.'://'.$client_url.'/api/common/edit/supplier';
                $params = [
                    'form_params' => $rec,
                    'headers' => [
                        'ident' => 'production'
                    ]
                ];
                $yewures = $this->guzzle_post($send_url, $params);
                $yewuresponse = json_decode($yewures->getBody()->getContents(), true);

                if($yewuresponse['code'] == 0){
                    return ['code' => 0, 'text' => '保存成功'];
                }else{
                    return ['code' => -1, 'text' => '保存失败'];
                }
                return ['code' => 0, 'text' => '保存成功'];
            }
        }
    }

}